
import { crtChannel } from "./crtChannel";
import { crtChannelWeb } from "./crtChannelWeb";
import crtkc from "./crtkc";
import { nativeInterAd } from "./nativeInterAd";


export class crtChannelHw extends crtChannelWeb {
    channelObj: any = (window as any).qg;
    paraMap: Map<String, string> = new Map([
        ['bannerId', 'j1pcnpx5tu'], // 正式 x5s8wsqgaa    测试 j1pcnpx5tu
        ['interId', 'testu7m3hc4gvm'],  // 正式 x04r7dm3bq   测试 u7m3hc4gvm
        ['videoAd', 'u2k89ub7vq'],  // 正式 i3y7pvqn2l   测试 u2k89ub7vq
        // ['htId', '2007752'],
        ['ctrlId', '6909b21b088d094169eb8bd1'],
        ['appId', '114911505'],

        ['gid', ''],
    ]);
    videoArr: string[] = ["u2k89ub7vq", "u2k89ub7vq", "u2k89ub7vq"];
    interArr: string[] = ["testu7m3hc4gvm", "testu7m3hc4gvm", "testu7m3hc4gvm"];
    //interArr: string[] = ["12345", "54321", "a5ntd3e3jl"];
    gameName: string = '羊哥大冒险';

    ruanzhu: string = '著作权人：南京天豆互动网络科技有限公司\n登记号：2022R0373302';

    /** 分渠道的广告特定动作，actId区分哪个动作,para为执行动作需要的对象
     * 1:补充体力、体力特权、补充星星这三个界面加文字“观看视频免费领取”，勾选状态下隐藏放弃按钮，不勾选则显示。0为不勾选状态，1为勾选状态
     * 2:二级页面弹出插屏  0:关闭 1:开启  p2控制  p3控制延迟
     * 3:恭喜过关之后九宫格  0:关闭 1:开启
     * 4:游戏失败后弹出九宫格..  0:关闭  1:开启
     * 5:不管输赢每赢3次,点击开始游戏后跳转视频,中途关闭不能继续玩,0为关闭,1为开启
     * 6:每连赢两关,恭喜过关后弹宝箱, 0为关闭  1为开启
     * 7:狂点宝箱弹插屏
     * 更改日期 2025/3/11
     */
    OptionsSync = null
    isBuyUser = false
    startFlag = true
    init(callback?: any): void {
        const me = this;
        const ctrlId = me.getPara('ctrlId');
        console.error("===> init: ctrlId: ", ctrlId);
        if (ctrlId) {
            try {
                this.OptionsSync = me.channelObj.getLaunchOptionsSync()
                console.log("启动信息", JSON.stringify(this.OptionsSync))
                let query: string = this.OptionsSync.query

                if (query && query.indexOf("callback") >= 0) {
                    console.log("query信息", query)
                    let callback: String = JSON.parse(query).callback
                    console.log("callback信息", callback)
                    if (callback) {
                        this.pValue = callback.split('&')[2]
                        if (this.pValue.length > 10) {
                            console.log("校验成功", this.pValue)
                            this.isBuyUser = true
                        }
                        console.log("有callback", this.pValue)
                    } else {
                        console.log("没有callback")
                    }
                } else {
                    this.isBuyUser = false
                }
            } catch {
                console.error("买量代码出现错误")
            }
            let reqData = null
            if (false) {
                reqData = {
                    'id': ctrlId,
                    'fromid': 1,
                }
            } else {
                reqData = {
                    'id': ctrlId
                }
            }
            try {
                if (this.isBuyUser) {
                    setTimeout(() => {
                        crtkc.eventReport("游戏时间10")
                        console.error("1游戏时间10")
                    }, 10 * 1000);
                    setTimeout(() => {
                        crtkc.eventReport("游戏时间20")
                        console.error("1游戏时间20")
                    }, 20 * 1000);
                    setInterval(() => {
                        crtkc.eventReport("游戏时间" + this.timeIndex * 30)
                        console.error("1游戏时间" + this.timeIndex * 30)
                        this.timeIndex++
                    }, 30 * 1000);
                } else {
                    setTimeout(() => {

                        console.error("游戏时间10")
                    }, 10 * 1000);
                    setTimeout(() => {

                        console.error("游戏时间20")
                    }, 20 * 1000);
                    setInterval(() => {

                        console.error("游戏时间" + this.timeIndex * 30)
                        this.timeIndex++
                    }, 30 * 1000);
                }
            } catch {
                console.error("游戏时间上报错误")
            }
            crtkc.jpost('https://ga.gametdd.com/adCtrl/agd/conf', reqData, (err, re) => {
                if (err) {
                    console.error("错误: ", err);
                    if (callback) {
                        callback();
                    }
                    return;
                }
                if ('' + re.code !== '0') {
                    console.error('返回值错误:' + JSON.stringify(re));
                    if (callback) {
                        callback();
                    }
                    return;
                }
                me.ctrl = re.data;
                console.error('====>返回', me.ctrl);

                if (callback) {
                    callback();
                }

                this.loadvideoflag = true
                try {
                    this.channelObj.onHide(
                        () => {
                            console.error("onHide", this.rewordVideo, this.onHideVideo, this.loadvideoflag, this.startFlag)
                            if (this.rewordVideo || this.onHideVideo || this.loadvideoflag || this.startFlag) {
                                return
                            } else {
                                // if (this.getCtrl('p26') > Math.random() * 100) {
                                //     this.onHideVideos(this.getCtrl('p27'))
                                // }

                                if (this.getCtrl('p14')) {
                                    console.log("this.existInter)", this.existInter)
                                    if (!this.existInter) {
                                        this.onHideVideos(this.getCtrl('p14'))
                                    }
                                }
                            }
                        }
                    );
                } catch {
                    console.error("onHide出现问题")
                }


            });
            this.reLoadVideo();
        }
        this.getOAId()
    }
    winCnt: number = 0;
    action5Cnt: number = 0;
    loadingProgessArray = []
    loadingFlagArray = []
    timeIndex = 1
    clickcount = 0
    inGameInterInterval = null
    pValue = ""
    A = true
    B = true
    doAction(actId: any, para: any) {

        if (para && para[0] == 'hw') {
            if (actId == '玩家没有任何操作') {

            } else {
                console.log("hw策略名字", actId, "参数", para)
            }
            if (actId == "二级界面弹出插屏") {
                let isOpen = this.getCtrl('p1') > Math.random() * 100
                if (isOpen) {

                    this.showInter()
                }
            } else if (actId == "常驻banner") {
                setTimeout(() => {
                    this.startFlag = false
                }, 3000);
                let isOpen = this.getCtrl("p2") > Math.random() * 100
                if (isOpen && this.B) {
                    this.B = false
                    setTimeout(() => {

                        this.showBanner()
                    }, 2000);
                }
            } else if (actId == 'AD视频标识') {
                let isOpen = false
                if (isOpen) {
                    para[1].active = false
                    para[2].active = true
                } else {
                    para[1].active = true
                    para[2].active = false
                }

            } else if (actId == '插屏间隔') {

                setTimeout(() => {
                    this.canShowInter = true
                    console.error("间隔结束")
                }, 1000);
            }

            else if (actId == '同意隐私政策强弹视频') {
                let isOpen = this.getCtrl('p3')
                if (isOpen) {
                    this.loadingVideoA(this.getCtrl('p3'))
                }
            } else if (actId == "loading结束强弹视频广告") {

                this.loadvideoflag = true
                let isOpen = this.getCtrl('p5')
                if (isOpen) {

                    this.loadingVideo(this.getCtrl('p5'))
                }
            }

            else if (actId == "体力特权/体力补给站页面") {
                let isOpen = this.getCtrl("p7")
                if (isOpen == 1) {
                    para[1].opacity = 255
                } else if (isOpen == 2) {
                    para[1].opacity = 0
                }
            }
            else if (actId == "点击按钮弹视频") {
                let isOpen = this.getCtrl("p8")

                if (isOpen) {
                    this.loadingVideo(this.getCtrl('p8'))
                }

            }
            else if (actId == "每关游戏开始弹插屏，主页弹插屏") {
                let isOpen = this.getCtrl("p9") > Math.random() * 100
                if (isOpen) {
                    this.showInter()
                }
            }
            else if (actId == "画线的十秒内强弹视频") {
                let isOpen = this.getCtrl("p10")

                if (isOpen) {
                    setTimeout(() => {
                        this.loadingVideo(this.getCtrl('p10'))
                    }, 3000);
                }

            }
            else if (actId == "过关页面的下一关") {
                let isOpen = this.getCtrl("p11")
                if (isOpen == 1) {
                    para[1].opacity = 255
                } else if (isOpen == 2) {
                    para[1].opacity = 0
                }

            } else if (actId == "返回到主页面强弹视频") {
                let isOpen = this.getCtrl("p12")
                if (isOpen) {
                    this.loadingVideo(this.getCtrl('p12'))
                }

            } else if (actId == '插屏一定会被点击') {
                let isOpen = this.getCtrl("p13") > Math.random() * 100
                console.log("插屏一定会被点击", isOpen)
                if (isOpen) {
                    para[1](true)
                } else {
                    para[1](false)
                }
            } else if (actId == '有插屏') {
                this.existInter = true
            } else if (actId == '无插屏') {
                setTimeout(() => {
                    this.existInter = false
                }, 2000);

            }
            // else if (actId == "开始游戏下一关关闭按钮") {

            //     let isOpen = this.getCtrl('p8')
            //     if (isOpen) {
            //         this.loadingVideo(this.getCtrl('p8'))
            //     }
            // } else if (actId == "每关开始游戏强弹插屏") {

            //     let isOpen = this.getCtrl('p9')
            //     if (isOpen) {
            //         this.showInter()
            //     }
            // } else if (actId == "点击次数强弹视频") {
            //     let isOpen = this.getCtrl('p10')
            //     this.clickcount++
            //     if (isOpen && this.clickcount >= this.getCtrl('p10')) {
            //         this.clickcount = 0
            //         this.loadingVideo(this.getCtrl('p11'))
            //     }
            // } else if (actId == "重置点击次数") {
            //     this.clickcount = 0
            //     clearInterval(this.inGameInterInterval)
            //     this.A = true
            // } else if (actId == "局内强弹插屏") {

            //     let isOpen = this.getCtrl('p13') > Math.random() * 100
            //     if (isOpen && this.A) {
            //         this.A = false
            //         this.inGameInterInterval = setInterval(() => {
            //             this.showInter()
            //         }, this.getCtrl('p12') * 1000);
            //     }
            // } else if (actId == '插屏一定会被点击') {
            //     let isOpen = this.getCtrl("p15") > Math.random() * 100
            //     console.log("插屏一定会被点击", isOpen)
            //     if (isOpen) {
            //         para[1](true)
            //     } else {
            //         para[1](false)
            //     }
            // } else if (actId == '有插屏') {
            //     this.existInter = true
            // } else if (actId == '无插屏') {
            //     setTimeout(() => {
            //         this.existInter = false
            //     }, 2000);

            // } else if (actId == "返回到主页强弹视频") {

            //     let isOpen = this.getCtrl('p17')
            //     if (isOpen) {
            //         this.loadingVideo(this.getCtrl('p17'))
            //     }
            // }
        }
    }

    Ainterval = null
    clickTime = 0
    doActionCanshowInter = true
    getRandomFloat(min: number, max: number): number {
        return Math.random() * (max - min) + min;
    }
    showManyVideo(succ, end = null): void {

        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            console.log("观看广告")
            succ && succ()
            end && end()
            return
        }
        console.log("观看广告")
        // this.rewordVideo = true
        this.videoEnd = end
        this.videoSuccess = succ

        this.rewardedVideoAd.show();
        this.rewardedVideoAd.load();
    }

    onHideChangeFlag = true
    onHideVideos(times) {
        if (times === 0) {

            // this.onHideVideo = false

            return; // 终止递归
        }
        if (this.onHideChangeFlag) {
            this.onHideChangeFlag = false
            this.onHideVideo = true
            console.error("---------------------onHideVideos", times)
            setTimeout(() => {
                this.onHideVideo = false
                this.onHideChangeFlag = true

            }, 1000 * this.getCtrl("p6") * (this.getCtrl('p14') + 1) + 2000);
        }
        this.showManyVideo(() => {

        }, () => {
            setTimeout(() => {
                this.onHideVideos(times - 1); // 递归调用
            }, 1000 * this.getCtrl("p6"));
        }
        );
    }
    loadvideoflag = false
    loadingVideoChangeFlag = true
    loadingVideo(times) {
        if (times === 0) {

            //this.loadvideoflag = false

            return; // 终止递归
        }
        if (this.loadingVideoChangeFlag) {
            this.loadingVideoChangeFlag = false
            this.loadvideoflag = true
            console.error("---------------------loadingVideo", times, 1000 * this.getCtrl("p6") * (times + 1) + 500)
            setTimeout(() => {
                this.loadingVideoChangeFlag = true
                this.loadvideoflag = false

            }, 1000 * this.getCtrl("p6") * (times + 1) + 1000);
        }
        this.showManyVideo(() => {

        }, () => {
            setTimeout(() => {
                this.loadingVideo(times - 1); // 递归调用
            }, 1000 * this.getCtrl("p6"));
        }
        );
    }

    loadingVideoA(times) {
        if (times === 0) {

            this.loadvideoflag = false

            return; // 终止递归
        }
        this.loadvideoflag = true


        if (this.loadingVideoChangeFlag) {
            this.loadingVideoChangeFlag = false
            this.loadvideoflag = true
            console.error("---------------------loadingVideo", times, 1000 * this.getCtrl("p4") * (times + 1) + 500)
            setTimeout(() => {
                this.loadingVideoChangeFlag = true
                this.loadvideoflag = false

            }, 1000 * this.getCtrl("p4") * (times + 1) + 1000);
        }
        this.showManyVideo(() => {

        }, () => {
            setTimeout(() => {
                this.loadingVideoA(times - 1); // 递归调用
            }, 1000 * this.getCtrl("p4"));
        }
        );
    }
    levelCount = 0
    clickCount = 0

    exit() {
        this.channelObj.exitApplication({
            success: function () {
                console.log("exitApplication success");
            },
            fail: function () {
                console.log("exitApplication fail");
            },
            complete: function () {
                console.log("exitApplication complete");
            }
        });
    }

    hasPrivacy(): boolean {
        if (crtkc.getData('isAgree', 0) == 1) {
            return true;
        }
        return false;
    }

    rewordVideo = false
    onHideVideo = false
    existInter = false
    hwOAID = null
    showLoginUI() {
        cc.resources.load("prefab/popup/Login", cc.Prefab, (err, prefab) => {
            let node = cc.instantiate(prefab)

            node.parent = cc.find("Canvas")
        })
    }
    getOAId() {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            console.log("获得oaId")
            return
        }
        //获取设备oaid
        if (window["qg"].getOAID) {
            window["qg"].getOAID({
                success: (ret) => {
                    let oaid = ret["oaid"];
                    console.log("qg.getOAID success :" + JSON.stringify(ret));
                    console.log("======oaid oaid oaid: ====", oaid);
                    this.hwOAID = oaid;
                    const reqData = { 'oaid': this.hwOAID, 'p': this.pValue };
                    console.log("======p:====", this.getPara('p'));
                    crtkc.jpost('https://ga.gamejym.com/fcm/adTrack/hw_active', reqData, (err, re) => {
                        if (err) {
                            console.error(err);
                            return;
                        }
                        console.error('关键行为激活', JSON.stringify(re));
                    });
                },
                fail: (errMsg, errCode) => {
                    console.error("qg.getOAID fail " + errCode + ': ' + errMsg)
                },
                complete: () => {
                    console.error("qg.getOAID complete")
                }
            })
        }
    }

    qgLogin(success, fail) {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            //fail()
            success()
            return
        }
        const me = this;
        console.log("Sign-in", this.getPara('appId'));
        this.channelObj.gameLoginWithReal({
            forceLogin: 1,
            appid: me.getPara('appId'),
            success: function (data) {
                // 登录成功后，可以存储帐号信息。             
                console.log(" game login with real success:" + JSON.stringify(data));
                //me.channelUserId = data.playerId;
                success && success("login succ");

                // if (pzaLocalData.data.playerNick != null && pzaLocalData.data.playerAvatar != null) {
                //     return;
                // }
                // me.sendUserInfo({nickName:data.displayName,avatarUrl:data.imageUri})
            },
            fail: function (data, code) {
                console.log("game login with real fail:" + data + ", code:" + code);
                //根据状态码处理游戏的逻辑。
                //状态码为7004或者2012，表示玩家取消登录。
                //此时，建议返回游戏界面，可以让玩家重新进行登录操作。
                if (code == 7004 || code == 2012) {
                    console.log("玩家取消登录，返回游戏界面让玩家重新登录。")
                    fail && fail("login fail");
                }
                //状态码为7021表示玩家取消实名认证。
                //在中国大陆的情况下，此时需要禁止玩家进入游戏。
                if (code == 7021) {
                    console.log("The player has canceled identity verification. Forbid the player from entering the game.")
                    fail && fail("login fail");
                }
                fail && fail("login fail");
            }
        })
    }
    levelTimes = 0 //玩的关卡次数

    getSysInfo() {

        if (!this.sysInfo) {
            this.sysInfo = this.channelObj.getSystemInfoSync();
        }
        return this.sysInfo;
    }
    bannerAD: any = null;
    bannerCanShow = true
    bannerAdID
    nativeBanner = null
    showBanner(isForce?: boolean, styleFn?: any, where?: string): void {
        // if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
        //     cc.resources.load("prefab/popup/NativeBannerAd", cc.Prefab, (err, prefab) => {
        //         let node = cc.instantiate(prefab)
        //         //node.getComponent(nativeInterAd).init(test.adList[0].adId, imgUrlList, test.adList[0].title, test.adList[0].source, test.adList[0].clickBtnTxt)
        //         node.parent = cc.find("Canvas")
        //     })
        //     return
        // }
        // let that = this
        // this.nativeBanner = this.channelObj.createNativeAd({
        //     adUnitId: this.getPara("bannerId"),
        //     success: function (code) {
        //         console.log("loadBannerAd loadBannerAd : success");
        //     },
        //     fail: function (data, code) {
        //         console.log("loadBannerAd loadBannerAd fail: " + data + "," + code);
        //     },
        //     complete: function () {
        //         console.log("loadBannerAd loadBannerAd : complete");
        //     }
        // });
        // this.nativeBanner.offLoad();
        // this.nativeBanner.onLoad(function (test) {
        //     console.log("ad demo loadBannerAd onLoad NativeAd enter length : " + test.adList.length);
        //     console.log(JSON.stringify(test));
        //     var imgUrlList = test.adList[0].imgUrlList;
        //     console.log("ad demo : loadBannerAd onLoad imgUrlList : " + imgUrlList);
        //     cc.resources.load("prefab/popup/NativeBannerAd", cc.Prefab, (err, prefab) => {
        //         let node = cc.instantiate(prefab)
        //         node.getComponent(nativeInterAd).init(test.adList[0].adId, imgUrlList, test.adList[0].title, test.adList[0].source, test.adList[0].clickBtnTxt, true)
        //         node.parent = cc.find("Canvas")
        //     })


        // });
        // this.nativeBanner.offError();
        // this.nativeBanner.onError(function (test) {
        //     console.log("ad demo : loadBannerAd onError enter" + test.errCode);
        //     console.log("ad demo : loadBannerAd onError enter" + test.errMsg);
        //     setTimeout(() => {
        //         that.showBanner()
        //     }, 30000);

        // });
        // this.bannerAdID = this.nativeBanner.load();
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            console.error("展示banner")
            return
        }
        //console.log("展示banner")
        if (!this.bannerCanShow) {
            return
        }
        this.bannerCanShow = false
        //获取手机详细参数
        let that = this
        var sysInfo = this.channelObj.getSystemInfoSync();
        //console.log("on getSystemInfoSync: success =" + JSON.stringify(sysInfo));
        //获取当前手机屏幕高度(dp)
        var bannerTop = sysInfo.safeArea.height
        this.bannerAD = this.channelObj.createBannerAd({
            // console.log("createBannerAd");          
            adUnitId: this.getPara("bannerId"),
            // adIntervals: 30,
            style: {
                //top需要手机屏幕高度减去广告本身高度
                top: bannerTop - 57,
                left: 0,
                height: 57,
                width: 360,
            }
        });
        setTimeout(function () {
            that.bannerAD.show()
        }, 1000);
        this.bannerAD.onClose(() => {
            setTimeout(() => {
                that.bannerCanShow = true
                that.showBanner()
            }, 30000);
        })
    }


    videIndex: number = 0;

    showAdSucCnt: number = 0;
    rewardedVideoAd;
    videoSuccess = null
    videoEnd = null
    //videoError = null
    reLoadVideo() {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            return
        }
        console.error('videoInde', this.videIndex)
        this.rewardedVideoAd = this.channelObj.createRewardedVideoAd({
            adUnitId: this.videoArr[this.videIndex],
            success: (code) => {
                console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd success");
            },
            fail: (data, code) => {
                console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd fail: " + data + "," + code);
            },
            complete: () => {
                console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd complete");
            }
        });
        this.rewardedVideoAd.offLoad()
        this.rewardedVideoAd.onLoad(() => {
            console.log('ad demo :ad loaded.')
        })
        let taht = this
        this.rewardedVideoAd.offError()
        this.rewardedVideoAd.onError((e) => {
            console.error('load ad error:' + JSON.stringify(e));
            const errCode = e.errCode
            const errMsg = e.errMsg
            // this.channelObj.showToast({
            //     title: '广告正在加载，请稍后',
            // });

            if (taht.videIndex < 2) {
                taht.videIndex++
            } else {
                taht.videIndex = 0
                console.log("重置广告位index", this.videIndex)
            }
            if (taht.videIndex == 0) {
                console.log("停止", this.videIndex)
                this.channelObj.showToast({
                    title: '广告正在加载，请稍后',
                });
                setTimeout(() => {
                    console.error("重新循环广告")
                    taht.reLoadVideo()
                }, 5000);

            } else {

                console.log("重新reload", this.videIndex)
                taht.reLoadVideo()
                console.error("重新换广告位")
            }
            this.videoEnd && this.videoEnd()

            // this.rewordVideo = false

            // this.videoError && this.videoError()
        })
        this.rewardedVideoAd.offClose()
        this.rewardedVideoAd.onClose((res) => {
            // if(!this.rewardedVideoAd) return
            // this.rewardedVideoAd.offClose(null);
            console.log('ad onClose: ' + res.isEnded)
            // this.rewardedVideoAd.offLoad(null);
            if (res && res.isEnded || res === undefined) {
                console.log('播放激励视频结束，给予奖励')
                this.videoSuccess && this.videoSuccess()
                this.videoEnd && this.videoEnd()
            } else {
                console.log('播放没结束，不给予奖励')
                this.videoSuccess && this.videoSuccess()
                this.videoEnd && this.videoEnd()
            }
            this.sendReportVideoActive(res.isEnd)

            // this.rewordVideo = false

        })
        this.rewardedVideoAd.load()
    }
    sendReportVideoActive(isEnd) {
        if (!this.hwOAID) {
            console.error("this.hwOAID为空")
            return;
        }
        const reqData = { 'oaid': this.hwOAID, 'p': this.pValue, 'isEnd': isEnd };
        crtkc.jpost('https://ga.gamejym.com/fcm/adTrack/hw_addiction', reqData, (err, re) => {
            if (err) {
                console.error(err);
                return;
            }
            console.error('关键行为视频返回', JSON.stringify(re));
        });
    }
    sendReportActive(adKey, isbanner = false) {
        if (!this.hwOAID) {
            console.error("this.hwOAID为空")
            return;
        }
        const reqData = { 'oaid': this.hwOAID, 'p': this.pValue };
        let that = this
        crtkc.jpost('https://ga.gamejym.com/fcm/adTrack/hw_addiction', reqData, (err, re) => {
            if (err) {
                console.error(err);
                return;
            }
            console.log('reportAdClick:', adKey)
            if (isbanner) {
                that.nativeBanner.reportAdClick({
                    adId: adKey,
                })
            } else {
                that.nativeAd.reportAdClick({
                    adId: adKey,
                })
            }

            console.error('关键行为插屏返回', JSON.stringify(re));
        });
    }
    //由于激励广告要求预加载，可在进入游戏时立即触发上述逻辑。在onLoad触发成功回调时，可以展示视频广告组件。
    //玩家每次点击视频按钮时调用rewardedVideoAd.show()播放广告。在播放期间或者关闭视频前调用rewardedVideoAd.load()请求下一次广告
    showVideo(succ, end = null): void {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            succ()
            end && end()
            console.error("观看广告完毕")
            return
        }
        this.rewordVideo = true
        this.videoEnd = end
        this.videoSuccess = succ
        this.rewardedVideoAd.show();
        this.rewardedVideoAd.load();
        setTimeout(() => {
            this.rewordVideo = false
        }, 2000);
    }

    /** 插屏间隔秒数 */
    interGap: number = 30 * 1000;
    /** 下次可出插屏的时间 */
    nextInterTime: number = 0;

    nativeAd = null;
    adId = null
    canShowInter = true
    interIndex = 0
    showInter(show = true): void {
        if (!this.canShowInter) {
            console.log("插屏间隔中")
            return
        }
        if (cc.find("Canvas/NativeInterAdLong")) {
            console.log("Canvas/NativeInterAdLong")
            return
        }
        if (cc.find("Canvas/NativeInterAd")) {
            console.log("Canvas/NativeInterAd")
            return
        }
        this.canShowInter = false
        // setTimeout(() => {
        //     if (find("Canvas/NativeInterAd") || find("Canvas/NativeInterAdLong")) {
        //     } else {
        //         console.log("插屏间隔保险")
        //         this.canShowInter = true
        //     }
        // }, 5000);
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            console.log("插屏展示", show)
            cc.resources.load("prefab/popup/NativeInterAd", cc.Prefab, (err, prefab) => {
                let node = cc.instantiate(prefab)
                //node.getComponent(nativeInterAd).init(test.adList[0].adId, imgUrlList, test.adList[0].title, test.adList[0].source, test.adList[0].clickBtnTxt)
                node.parent = cc.find("Canvas")
            })
            return
        }

        console.log("展示插屏广告位", this.interArr[this.interIndex]);
        let that = this
        this.nativeAd = null
        this.nativeAd = this.channelObj.createNativeAd({
            adUnitId: this.interArr[this.interIndex],
            success: function (code) {
                console.log("loadNativeAd loadNativeAd : success");
            },
            fail: function (data, code) {
                console.log("loadNativeAd loadNativeAd fail: " + data + "," + code);
            },
            complete: function () {
                console.log("loadNativeAd loadNativeAd : complete");
            }
        });
        this.nativeAd.offLoad();
        this.nativeAd.onLoad(function (test) {
            console.log("ad demo loadNativeAd onLoad NativeAd enter length : " + test.adList.length);
            console.log(JSON.stringify(test));
            var imgUrlList = test.adList[0].imgUrlList;
            console.log("ad demo : loadNativeAd onLoad imgUrlList : " + imgUrlList);

            cc.resources.load("prefab/popup/NativeInterAd", cc.Prefab, (err, prefab) => {
                let node = cc.instantiate(prefab)
                node.getComponent(nativeInterAd).init(test.adList[0].adId, imgUrlList, test.adList[0].title, test.adList[0].source, test.adList[0].clickBtnTxt)
                node.parent = cc.find("Canvas")
            })
            that.interIndex = 0
        });
        this.nativeAd.offError();
        this.nativeAd.onError(function (test) {
            if (that.interIndex >= 2) {
                console.log("停止了")
                that.canShowInter = true
                setTimeout(() => {
                    that.interIndex = 0
                }, 2000);
                //that.interIndex = 0
            } else {
                that.interIndex++
                console.log("重新换插屏位", that.interIndex)
                that.canShowInter = true
                that.showInter()
            }


            console.log("ad demo : loadNativeAd onError enter" + test.errCode);
            console.log("ad demo : loadNativeAd onError enter" + test.errMsg);
            that.nativeAd.destroy()
        });
        this.adId = this.nativeAd.load();
    }

    privacyAgree(): number {
        if (!crtkc.getData('isAgree', 0)) {
            crtkc.saveData('isAgree', 1);
        }
        return 1;
    }
    reportAdShow(adKey: string, isbanner = false): void {
        const reqData = { 'oaid': this.hwOAID, 'p': this.pValue };
        crtkc.jpost('https://ga.gamejym.com/fcm/adTrack/hw_adShow', reqData, (err, re) => {
            if (err) {
                console.error(err);
                return;
            }
            console.error('关键行为展示', JSON.stringify(re));
        });
        console.log('reportAdShow:', adKey, isbanner)
        if (isbanner) {
            this.nativeBanner.reportAdShow({
                adId: adKey
            })
        } else {
            this.nativeAd.reportAdShow({
                adId: adKey
            })
        }
    }

    reportAdClick(adKey: string, isbanner = false): void {

        this.sendReportActive(adKey, isbanner)
    }



}